package cn.qin.DAO.impl;

import java.util.List;

import net.sf.json.JSONArray;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.json.JSONException;

import cn.qin.DAO.newsOperateDAO;
import cn.qin.Models.NEWS;
import cn.qin.Models.admin;

public class newsOperateDAOimpl implements newsOperateDAO {
	private static SessionFactory  sessionFactory;
	@SuppressWarnings("deprecation")
	public void saveNews(NEWS news) {	
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		news.setId(news.getId());
		news.setTitle(news.getTitle());
		news.setContent(news.getContent());
		news.setDate(news.getDate());
		System.out.println("save success!"+news.getTitle());
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(news);
		session.getTransaction().commit();
		sessionFactory.close();	
	}
	
	//删除新闻，参数id是新闻的id
	public void delNews(int id) {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query q = session.createQuery(" delete FROM NEWS WHERE id="+id);
		q.executeUpdate();
		System.out.println("newsOperateDAOimpl:public JSONArray showNews(NEWS news)");
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
	
	//显示新闻BY id DESC
	public JSONArray showNews(NEWS news) throws JSONException {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			Query q = session.createQuery("FROM NEWS ORDER BY id DESC ");
			List list = q.list();
			JSONArray json = JSONArray.fromObject(list);
			System.out.println("newsOperateDAOimpl:public JSONArray showNews(NEWS news)");
			//System.out.println(json);
			session.getTransaction().commit();
			sessionFactory.close();
			return json;
		}
	public boolean login(admin admin){
		boolean b=false;
		SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query q = session.createQuery("FROM admin WHERE name=?");
		q.setString(0, admin.getName());
		System.out.println(admin.getName());
		System.out.println(admin.getPassword());
		//list类型转换成admin类型
		List<admin> list =(List)q.list();
		/*
		 *根据传进来的用户名admin.getName()查找数据库，当查找返回的集合不为空时，取得查询的值
		 *然后和前台传进来的数据进行比较，如果相等，登陆成功。
		 */
		if((list!=null)&&(list.size()>0)){
			String admin_name =list.get(0).getName();
			String admin_password=list.get(0).getPassword();
			if(admin_name.equals(admin.getName())&&admin_password.equals(admin.getPassword())){
				System.out.println("登陆成功！");
				b=true;
				
			}else{
				System.out.println("登录失败，请检查帐号密码，然后重新登录！");
				b=false;
				}
		}
		else 
		{
			System.out.println("没找到用户，请重新检查输入");
		b=false;
		}
		
		if(b)
			return true;
		else 
			return false;

			
		//session.close();
		//sessionFactory.close();	
	}
	}
	


